﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Data;
using System.Data.SqlClient;
using System.Configuration;
using System.ComponentModel;


namespace FinalCRM.Mary_s_Modules
{
    [DataObject]
    public class retrieveCustomerName
    {
        [DataObjectField(true, true)]
        public string CustomerName { get; set; }
        public string RelatedCus { get; set; }

        [DataObjectMethod(DataObjectMethodType.Select)]
        public static List<retrieveCustomerName> addCusName()
        {
            List<retrieveCustomerName> CusList = new List<retrieveCustomerName>();
            SqlConnection conn = null;
            try
            {
                conn.ConnectionString = "Data Source=.;Initial Catalog=FCRM;Integrated Security";
                conn.Open();
                SqlCommand comm = new SqlCommand();
                comm.Connection = conn;
                comm.CommandText = "SELECT * from CustomerAccount";
                SqlDataReader dr = comm.ExecuteReader();
                while (dr.Read())
                {
                    retrieveCustomerName rUD = new retrieveCustomerName();
                    rUD.CustomerName = (string)dr["CustomerID"];
                    rUD.RelatedCus = (string)dr["CustomerName"];
                }
            }
            catch (SqlException e)
            {
                throw e;
            }
            finally 
            {
                conn.Close();
            }
            return CusList;
        }
    }
    public class DocumentObj
    {     

        public string DocName {get; set;}
        public string FileName {get; set;}
        public string DocType {get; set;}
        public string CustomerName {get; set;}
        public string UserName {get; set;}
        public DateTime DateCreated {get; set;}
        public string Description {get; set;}

        
        public static void CreateDoc(DocumentObj d)
        {
            SqlConnection con = null;
            try
            {
                con.ConnectionString = "Data Source=.;Initial Catalog=FCRM;Integrated Security=true";
                SqlCommand com = new SqlCommand();
                con.Open();
                com.Connection = con;
                com.CommandText = "INSERT INTO CusDocument(DocName,FileName,DocType,DateCreated,UserName,CustomerName,Description) VALUES (@DocName,@FileName,@DocType,@DateCreated,@UserName,@CustomerName,@Description)";
                com.Parameters.AddWithValue("@DocName", d.DocName);
                com.Parameters.AddWithValue("@FileName", d.FileName);
                com.Parameters.AddWithValue("@DocType", d.DocType);
                com.Parameters.AddWithValue("@DateCreated", d.DateCreated);
                com.Parameters.AddWithValue("@UserName", d.UserName);
                com.Parameters.AddWithValue("@CustomerName", d.CustomerName);
                com.Parameters.AddWithValue("@Description", d.Description);
                int affectedRows = com.ExecuteNonQuery();
            }
            catch (SqlException e)
            {
                throw e;
            }
            finally
            {
                con.Close();
            }
        }
        public static DataTable ViewDoc()
        {
            SqlConnection conn = null;
            DataTable dt = null;
            try
            {
                conn = new SqlConnection();
                conn.ConnectionString = "Data Source=.;Initial Catalog=FCRM;Integrated Security=true";
                SqlCommand com = new SqlCommand();
                conn.Open();
                com.Connection = conn;
                com.CommandText = "Select * from CusDocument";
                SqlDataReader dr = com.ExecuteReader();
                dt = new DataTable();
                dt.Load(dr);
                dr.Close();
            }
            catch (SqlException e)
            {
                       throw e;
            }
            finally 
            {
                conn.Close();
            }
            return dt;
        }
        public static bool UpdateDoc(DocumentObj d)
        {
            SqlConnection conn = null;
            bool check = false;
            try
            {
                conn.ConnectionString = "Data Source=.;Initial Catalog=FCRM;Integrated Security=true";
                SqlCommand com = new SqlCommand();
                conn.Open();
                com.Connection = conn;
                com.CommandText = "UPDATE CusDocument SET FileName=@FileName,DocType=@DocType,Description=@Description) VALUES (@DocName,@FileName,@DocType,@DateCreated,@UserName,@CustomerName,@Description)";

                com.Parameters.AddWithValue("@FileName", d.FileName);
                com.Parameters.AddWithValue("@DocType", d.DocType);
                com.Parameters.AddWithValue("@Description", d.Description);

                int rows = com.ExecuteNonQuery();
                check = true;
            }
            catch (SqlException e)
            {
                throw e;
            }
            finally
            {
                conn.Close();
            }
            return check;
        }
        public static void DeleteDoc(DocumentObj d)
        {
            SqlConnection conn = null;
            try
            {
                conn.ConnectionString = "Data Source=.;Initial Catalog=FCRM;Integrated Security=true";
                SqlCommand comm = new SqlCommand();
                conn.Open();
                comm.Connection = conn;
                comm.CommandText = "DELETE from CusDocument WHERE DocName=@DocName";
                comm.Parameters.AddWithValue("@DocName", d.DocName);
                comm.Parameters.AddWithValue("@FileName", d.FileName);
                comm.Parameters.AddWithValue("@DocType", d.DocType);
                comm.Parameters.AddWithValue("@Description", d.Description);

                int rowsAffected = comm.ExecuteNonQuery();
            }
            catch (SqlException e)
            {
                throw e;
            }
            finally {
                conn.Close();
            }
        }
    }
}